﻿using System.Collections.Generic;
using EFERP;
using HRM.DICTIONARY.DAL;
using System;
using System.Windows.Forms;

namespace HRM.DICTIONARY.UI
{
    public partial class xucShiftAdd : DevExpress.XtraEditors.XtraForm
    {private void cbNghiGiuaCa_CheckedChanged(object sender, EventArgs e)
        {
            grpNghiGiuaCa.Enabled = !grpNghiGiuaCa.Enabled;
        }

        private void cbTinhTangCaTruocGioLam_CheckedChanged(object sender, EventArgs e)
        {
            grpTinhTangCaTruocGioLam.Enabled = !grpTinhTangCaTruocGioLam.Enabled;
        }

        private void cbTinhTangCaSauGioLam_CheckedChanged(object sender, EventArgs e)
        {
            grpTinhTangCaSauGioLam.Enabled = !grpTinhTangCaSauGioLam.Enabled;
        }

        private dic_shift _obj;
        private readonly ICommon<dic_shift> _exeDic;
        private CloseOrNew _closeNew = CloseOrNew.Close;

        public xucShiftAdd(ICommon<dic_shift> exeDic)
        {
            InitializeComponent();
            this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Insert;
            this._obj = new dic_shift();
            SetDataIntoUI();
        }

        public xucShiftAdd(dic_shift currObj, ICommon<dic_shift> exeDic)
        {
            InitializeComponent();
            this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Update;
            this._obj = currObj;
            SetDataIntoUI();
        }

        private void xucShiftAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            TotalClose();
        }

        private void SetDataIntoUI()
        {
            if (_obj == null)
                return;
            switch (_exeDic.myAction){
                case Actions.Update:
                    txtMaCa.Text = _obj.shiftcode;
                    txtTenCa.Text = _obj.shiftname;
                    if (_obj.isovernight != null) cbCaSangDem.Checked = (bool)_obj.isovernight;
                    if (_obj.isnightdutyday != null) cbTrucDem.Checked = (bool)_obj.isnightdutyday;
                    timeGioVao.EditValue = _obj.begintime;
                    timeGioRa.EditValue = _obj.endtime;
                    timeGioVaoTu.EditValue = _obj.begintime1;
                    timeGioVaoDen.EditValue = _obj.begintime2;
                    timeGioRaTu.EditValue = _obj.endtime1;
                    timeGioRaDen.EditValue = _obj.endtime2;
                    calDiTre.EditValue = _obj.lateminute;
                    calVeSom.EditValue = _obj.earlyminute;
                    cbNghiGiuaCa.EditValue = _obj.isbreak;
                    cbNghiGiuaCaCoSangDem.EditValue = _obj.isbreakovernight;
                    timeGioBatDauNghiGiuaCa.EditValue = _obj.breakbegintime;
                    timeGioKetThucNghiGiuaCa.EditValue = _obj.breakendtime;
                    if (_obj.methodcheck != null) cboMethodCheck.SelectedIndex = (int)_obj.methodcheck;

                    memoMoTa.EditValue = _obj.description;
                    cbTinhTangCaTruocGioLam.EditValue = _obj.isbot;
                    calBeforeDuBaoNhieuPhutThiTangCa.EditValue = _obj.minimumminutebot;
                    calBeforeSoPhutTangCaToiDa.EditValue = _obj.maximumminutebot;
                    calBeforeCachGioBatDauBaoNhieuPhut.EditValue = _obj.distanceminutebot;
                    cbTinhTangCaSauGioLam.EditValue = _obj.isaot;
                    calAfterDuBaoNhieuPhutThiTangCa.EditValue = _obj.minimumminuteaot;
                    calAfterSoPhutTangCaToiDa.EditValue = _obj.maximumminuteaot;
                    calAfterCachGioBatDauBaoNhieuPhut.EditValue = _obj.distanceminuteaot;
                    calTongSoPhutTrongCaLamViec.EditValue = _obj.totalminute;
                    calTongSoGioTrongCaLamViec.EditValue = _obj.totalhour;
                    calSoNgayCongKhiLamViecDuCa.EditValue = _obj.workday;

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = false;
                    break;
                case Actions.Insert:_obj = new dic_shift();
                    txtMaCa.Text = _exeDic.NewID("SH");
                    txtTenCa.Text ="";
                    cbCaSangDem.Checked = false;
                    cbTrucDem.Checked = false;
                    timeGioVao.EditValue = @"06:00:00 AM";
                    timeGioRa.EditValue = @"05:00:00 PM";
                    timeGioVaoTu.EditValue = @"06:00:00 AM";
                    timeGioVaoDen.EditValue = @"07:00:00 AM";
                    timeGioRaTu.EditValue = @"05:00:00 PM";
                    timeGioRaDen.EditValue = @"05:30:00 PM";
                    calDiTre.EditValue = 0;
                    calVeSom.EditValue = 0;
                    cbNghiGiuaCa.EditValue = false;
                    cbNghiGiuaCaCoSangDem.EditValue = false;
                    timeGioBatDauNghiGiuaCa.EditValue = @"11:30:00 AM";
                    timeGioKetThucNghiGiuaCa.EditValue = @"01:00:00 PM";
                    cboMethodCheck.SelectedIndex =0;
                    memoMoTa.EditValue = "";
                    cbTinhTangCaTruocGioLam.EditValue = false;
                    calBeforeDuBaoNhieuPhutThiTangCa.EditValue = 60;
                    calBeforeSoPhutTangCaToiDa.EditValue = 60;
                    calBeforeCachGioBatDauBaoNhieuPhut.EditValue = 30;
                    cbTinhTangCaSauGioLam.EditValue = false;
                    calAfterDuBaoNhieuPhutThiTangCa.EditValue = 60;
                    calAfterSoPhutTangCaToiDa.EditValue = 60;
                    calAfterCachGioBatDauBaoNhieuPhut.EditValue = 30;
                    calTongSoPhutTrongCaLamViec.EditValue = 0;
                    calTongSoGioTrongCaLamViec.EditValue = 0;
                    calSoNgayCongKhiLamViecDuCa.EditValue = 1;

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
                case Actions.InsertMultiple:
                    _obj = new dic_shift();
                   txtMaCa.Text = _exeDic.NewID("SH");
                    txtTenCa.Text ="";
                    cbCaSangDem.Checked = false;
                    cbTrucDem.Checked = false;
                    timeGioVao.EditValue = @"06:00:00 AM";
                    timeGioRa.EditValue = @"05:00:00 PM";
                    timeGioVaoTu.EditValue = @"06:00:00 AM";
                    timeGioVaoDen.EditValue = @"07:00:00 AM";
                    timeGioRaTu.EditValue = @"05:00:00 PM";
                    timeGioRaDen.EditValue = @"05:30:00 PM";
                    calDiTre.EditValue = 0;
                    calVeSom.EditValue = 0;
                    cbNghiGiuaCa.EditValue = false;
                    cbNghiGiuaCaCoSangDem.EditValue = false;
                    timeGioBatDauNghiGiuaCa.EditValue = @"11:30:00 AM";
                    timeGioKetThucNghiGiuaCa.EditValue = @"01:00:00 PM";
                    cboMethodCheck.SelectedIndex = 0;
                    memoMoTa.EditValue = "";
                    cbTinhTangCaTruocGioLam.EditValue = false;
                    calBeforeDuBaoNhieuPhutThiTangCa.EditValue = 60;
                    calBeforeSoPhutTangCaToiDa.EditValue = 60;
                    calBeforeCachGioBatDauBaoNhieuPhut.EditValue = 30;
                    cbTinhTangCaSauGioLam.EditValue = false;
                    calAfterDuBaoNhieuPhutThiTangCa.EditValue = 60;
                    calAfterSoPhutTangCaToiDa.EditValue = 60;
                    calAfterCachGioBatDauBaoNhieuPhut.EditValue = 30;
                    calTongSoPhutTrongCaLamViec.EditValue = 0;
                    calTongSoGioTrongCaLamViec.EditValue = 0;
                    calSoNgayCongKhiLamViecDuCa.EditValue = 1;

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
            }
        }

        private bool SetUIIntoData()
        {
            if (txtMaCa.Text == "" || txtTenCa.Text == "")
            {
                MessageBox.Show(@"Vui lòng không để trống thông tin mã hoặc tên ca");
                return false;
            }
            _obj.shiftcode = txtMaCa.Text;
            _obj.shiftname = txtTenCa.Text ;
            _obj.isovernight = cbCaSangDem.Checked;
            _obj.isnightdutyday = cbTrucDem.Checked;
            _obj.begintime = timeGioVao.Time;
            _obj.endtime = timeGioRa.Time;
            _obj.begintime1 = timeGioVaoTu.Time;
            _obj.begintime2 = timeGioVaoDen.Time;
            _obj.endtime1 = timeGioRaTu.Time;
            _obj.endtime2 = timeGioRaDen.Time;
            _obj.lateminute = (int)calDiTre.Value;
            _obj.earlyminute = (int)calVeSom.Value;
            _obj.isbreak = cbNghiGiuaCa.Checked;
            _obj.isbreakovernight = cbNghiGiuaCaCoSangDem.Checked;
            _obj.breakbegintime = timeGioBatDauNghiGiuaCa.Time;
            _obj.breakendtime = timeGioKetThucNghiGiuaCa.Time;
            _obj.methodcheck = cboMethodCheck.SelectedIndex;
           _obj.description = memoMoTa.Text;
            _obj.isbot = cbTinhTangCaTruocGioLam.Checked;
            _obj.minimumminutebot = (int)calBeforeDuBaoNhieuPhutThiTangCa.Value;
            _obj.maximumminutebot = (int)calBeforeSoPhutTangCaToiDa.Value;
            _obj.distanceminutebot = (int)calBeforeCachGioBatDauBaoNhieuPhut.Value;
            _obj.isaot = cbTinhTangCaSauGioLam.Checked;
            _obj.minimumminuteaot = (int)calAfterDuBaoNhieuPhutThiTangCa.Value;
            _obj.maximumminuteaot = (int)calAfterSoPhutTangCaToiDa.Value;
            _obj.distanceminuteaot = (int)calAfterCachGioBatDauBaoNhieuPhut.Value;
            _obj.totalminute = (int)calTongSoPhutTrongCaLamViec.Value;
            _obj.totalhour = (double)calTongSoGioTrongCaLamViec.Value;
            _obj.workday = (double)calSoNgayCongKhiLamViecDuCa.Value;
            return true;
        }

        private void TotalControl()
        {
            bool kq;
            switch (_exeDic.myAction)
            {
                case Actions.Insert:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Insert;
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;

                case Actions.Update:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.UpdateOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Update;
                            this.Close();
                        }
                        else
                        {
                            _exeDic.ValueOfGridViewRow = null;
                            MessageBox.Show(@"Lỗi cập nhật dữ liệu vào hệ thống");
                        }
                    }
                    break;
                case Actions.InsertMultiple:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            _exeDic.myAction = Actions.InsertMultiple;
                            SetDataIntoUI();
                            this.DialogResult = DialogResult.OK;
                            if (_closeNew == CloseOrNew.Close)
                                this.Close();
                            else
                                _closeNew = CloseOrNew.New;
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }}
                    break;
            }
        }
        private void TotalClose()
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e){
            TotalClose();
        }

        private void btnLuuDong_Click(object sender, EventArgs e)
        {
            _closeNew = CloseOrNew.Close;
            TotalControl();
        }

        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            _exeDic.myAction = Actions.InsertMultiple;
            _closeNew = CloseOrNew.New;
            TotalControl();
        }
    }
}